package main

import "fmt"

//
func testIf() {
  a := 2
  if a == 1 {
    fmt.Printf("a=1\n")
  } else if a == 2 {
    fmt.Printf("a=2\n")
  } else if a == 3 {
    fmt.Printf("a=3\n")
  } else if a == 4 {
    fmt.Printf("a=4\n")
  } else {
    fmt.Printf("a=5\n")
  }
}

func testSwitch() {
  var a int = 2
  switch a {
  case 1:
    fmt.Printf("a=1\n")
  case 2:
    fmt.Printf("a=2\n")
  case 3:
    fmt.Printf("a=3\n")
  case 4:
    fmt.Printf("a=4\n")
  default:
    fmt.Printf("a=5\n")
  }
}

func getValue() int {
  return 6
}

func testSwitch2() {
  switch a := getValue(); a {
  case 1:
    fmt.Printf("a=1\n")
  case 2:
    fmt.Printf("a=2\n")
  case 3:
    fmt.Printf("a=3\n")
  case 4:
    fmt.Printf("a=4\n")
  default:
    fmt.Printf("invalid a =%d\n", a)
  }

  //print(a) //Unresolved reference 'a'
}

//一个case可以有多个值
func testSwitch3() {
  switch a := getValue(); a {
  case 1, 2, 3, 4, 5:
    fmt.Printf("a>=1 and a <= 5\n")
  case 6, 7, 8, 9, 10:
    fmt.Printf("a>=6 and a <=10\n")
  default:
    fmt.Printf("a > 10\n")
  }
}

//case可以写条件判断
func testSwitch4() {
  num := 75
  switch {
  case num >= 0 && num <= 25:
    fmt.Printf("a>=0 and a <= 25\n")
  case num > 25 && num <= 50:
    fmt.Printf("a>=25 and a <=50\n")
  case num > 50 && num <= 75:
    fmt.Printf("a>=50 and a <=75\n")
  default:
    fmt.Printf("a > 75\n")
  }
}

func testSwitch5() {
  num := 25
  switch {
  case num >= 0 && num <= 25:
    fmt.Printf("a>=0 and a <= 25\n")
    fallthrough //除了执行本case,还会执行下一个case
  case num > 25 && num <= 50:
    fmt.Printf("a>=25 and a <=50\n")
  case num > 50 && num <= 75:
    fmt.Printf("a>=50 and a <=75\n")
  default:
    fmt.Printf("a > 75\n")
  }
}


//go的switch是不需要加break的,自带
func testSwitch6() {
  num := 75
  switch {
  case num >= 0:
    fmt.Printf("a>=0 \n") //只会输出这个
  case num > 25:
    fmt.Printf("a>=25\n")
  case num > 50:
    fmt.Printf("a>=50 \n")
  default:
    fmt.Printf("a > 75\n")
  }
}


func main() {
  //testIf()

  //testSwitch()

  //testSwitch2()

  //testSwitch3()

  //testSwitch4()

  //testSwitch5()
/*
a>=0 and a <= 25
a>=25 and a <=50
*/

  testSwitch6() // a>=0
}
